package ru.ganyaev.vmrun;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

import ru.ganyaev.external_process.Command;

public class VmRunCommands {
    private Path vmRunPath;
    private String vmRun;
    private String hostedPlatform = "ws";

    public VmRunCommands(Path vmRunPath) {
        this.vmRunPath = vmRunPath;
        this.vmRun = this.vmRunPath.toString();
    }

    private String getPrepandCommand(String guestUser, String guestPassword) {
        // return frameQuotesIfNecessary(vmRunPath) + " -T " + hostedPlatform
        // + " -gu " + guestUser + " -gp " + guestPassword + " ";
        return null;
    }

    private String frameQuotesIfNecessary(String path) {
        String resultPath = path;
        if (path.contains(" ")) {
            resultPath = "\"" + path + "\"";
        }
        return resultPath;
    }

    public Command getStartCommand(String vmxPath) {
        return getStartCommand(vmxPath, UserInterfaces.GUI);
    }

    public Command getStartCommand(String vmxPath, UserInterfaces userInterface) {
        List<String> arguments = new ArrayList<String>();
        arguments.add("-T");
        arguments.add(hostedPlatform);
        arguments.add("start");
        arguments.add(vmxPath);
        arguments.add(userInterface.toString());

        return new Command(vmRun, arguments);
    }

    public Command getStopCommand(String vmxPath) {
        return getStopCommand(vmxPath, PowerOptions.SOFT);
    }

    public Command getStopCommand(String vmxPath, PowerOptions powerOption) {
        List<String> arguments = new ArrayList<String>();
        arguments.add("-T");
        arguments.add(hostedPlatform);
        arguments.add("stop");
        arguments.add(vmxPath);
        arguments.add(powerOption.toString());

        return new Command(vmRun, arguments);
    }

    public Command getResetCommand(String vmxPath) {
        return getResetCommand(vmxPath, PowerOptions.SOFT);
    }

    public Command getResetCommand(String vmxPath, PowerOptions powerOption) {
        List<String> arguments = new ArrayList<String>();
        arguments.add("-T");
        arguments.add(hostedPlatform);
        arguments.add("reset");
        arguments.add(vmxPath);
        arguments.add(powerOption.toString());
        return new Command(vmRun, arguments);
    }

    public Command getRevertToSnapshotCommand(String vmxPath,
            String snapshotName) {
        List<String> arguments = new ArrayList<String>();
        arguments.add("-T");
        arguments.add(hostedPlatform);
        arguments.add("revertToSnapshot");
        arguments.add(vmxPath);
        arguments.add(snapshotName);

        return new Command(vmRun, arguments);
    }

    public Command getRunProgramInGuestCommand(String vmxPath,
            String programName, List<String> programArguments,
            String guestUser, String guestPassword) {
        return getRunProgramInGuestCommand(vmxPath, programName,
                programArguments, guestUser, guestPassword, false);
    }

    public Command getRunProgramInGuestCommand(String vmxPath,
            String programName, List<String> programArguments,
            String guestUser, String guestPassword, Boolean isNoWait) {
        List<String> arguments = new ArrayList<String>();
        arguments.add("-T");
        arguments.add(hostedPlatform);
        arguments.add("-gu");
        arguments.add(guestUser);
        arguments.add("-gp");
        arguments.add(guestPassword);
        arguments.add("runProgramInGuest");
        arguments.add(vmxPath);
        if (isNoWait) {
            arguments.add("-noWait");
        }
        arguments.add("-activeWindow");
        arguments.add("-interactive");

        arguments.add(programName);
        arguments.addAll(programArguments);

        return new Command(vmRun, arguments);
    }

    public Command getRunScriptInGuestCommand(String vmxPath,
            String executorPath, String scriptText, String guestUser,
            String guestPassword) {
        return getRunScriptInGuestCommand(vmxPath, executorPath, scriptText,
                guestUser, guestPassword, false);
    }

    public Command getRunScriptInGuestCommand(String vmxPath,
            String executorPath, String scriptText, String guestUser,
            String guestPassword, Boolean isNoWait) {
        List<String> arguments = new ArrayList<String>();
        arguments.add("-T");
        arguments.add(hostedPlatform);
        arguments.add("-gu");
        arguments.add(guestUser);
        arguments.add("-gp");
        arguments.add(guestPassword);
        arguments.add("runScriptInGuest");
        arguments.add(vmxPath);
        if (isNoWait) {
            arguments.add("-noWait");
        }
        arguments.add(executorPath);
        arguments.add(scriptText);

        return new Command(vmRun, arguments);
    }

    public Command getListProcessesInGuestCommand(String vmxPath,
            String guestUser, String guestPassword) {
        List<String> arguments = new ArrayList<String>();
        arguments.add("-T");
        arguments.add(hostedPlatform);
        arguments.add("-gu");
        arguments.add(guestUser);
        arguments.add("-gp");
        arguments.add(guestPassword);
        arguments.add("listProcessesInGuest");
        arguments.add(vmxPath);

        return new Command(vmRun, arguments);
    }

    public Command getCopyFileFromHostToGuestCommand(String vmxPath,
            String hostFilePath, String guestFilePath, String guestUser,
            String guestPassword) {
        List<String> arguments = new ArrayList<String>();
        arguments.add("-T");
        arguments.add(hostedPlatform);
        arguments.add("-gu");
        arguments.add(guestUser);
        arguments.add("-gp");
        arguments.add(guestPassword);
        arguments.add("copyFileFromHostToGuest");
        arguments.add(vmxPath);
        arguments.add(hostFilePath);
        arguments.add(guestFilePath);

        return new Command(vmRun, arguments);
    }

    public Command getCopyFileFromGuestToHostCommand(String vmxPath,
            String guestFilePath, String hostFilePath, String guestUser,
            String guestPassword) {

        List<String> arguments = new ArrayList<String>();
        arguments.add("-T");
        arguments.add(hostedPlatform);
        arguments.add("-gu");
        arguments.add(guestUser);
        arguments.add("-gp");
        arguments.add(guestPassword);
        arguments.add("copyFileFromGuestToHost");
        arguments.add(vmxPath);
        arguments.add(guestFilePath);
        arguments.add(hostFilePath);

        return new Command(vmRun, arguments);
    }

    public String getDeleteDirectoryInGuestCommand(String vmxPath,
            String guestDirectoryPath, String guestUser, String guestPassword) {
        return getPrepandCommand(guestUser, guestPassword)
                + "deleteDirectoryInGuest " + frameQuotesIfNecessary(vmxPath)
                + " " + frameQuotesIfNecessary(guestDirectoryPath);
    }

    public Command getCreateDirectoryInGuestCommand(String vmxPath,
            String guestDirectoryPath, String guestUser, String guestPassword) {
        List<String> arguments = new ArrayList<String>();
        arguments.add("-T");
        arguments.add(hostedPlatform);
        arguments.add("-gu");
        arguments.add(guestUser);
        arguments.add("-gp");
        arguments.add(guestPassword);
        arguments.add("createDirectoryInGuest");
        arguments.add(vmxPath);
        arguments.add(guestDirectoryPath);

        return new Command(vmRun, arguments);
    }

    public String getCaptureScreenCommand(String vmxPath, String hostPath,
            String guestUser, String guestPassword) {
        return getPrepandCommand(guestUser, guestPassword) + "captureScreen "
                + frameQuotesIfNecessary(vmxPath) + " "
                + frameQuotesIfNecessary(hostPath);
    }
}